package com.dby.controller;

import com.dby.comm.ResultMessage;
import com.dby.entry.BlogUser;
import com.dby.service.BlogUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class BlogUserController {
        @RequestMapping("/admin/updpwd")
        public String index() {
            return "/admin/pwdmodify";
        }
        @RequestMapping("/admin/userlist")
        public String userlist() {
            List<BlogUser> blogUserList = blogUserService.getBlogUserByPage();
            httpSession.setAttribute("blogUserList",blogUserList);
            return "/admin/usermanager";
        }
        @RequestMapping("/admin/addbloguser")
        @ResponseBody
        public ResultMessage addbloguser(@RequestBody BlogUser blogUser) {
            blogUser.setCreateTime(new Date());
            blogUserService.addBlogUser(blogUser);
            return ResultMessage.success();
        }
        @Autowired
        HttpSession httpSession;
        @Autowired
        BlogUserService blogUserService;
        @RequestMapping("/admin/updpwdAction")
        @ResponseBody
        public ResultMessage updpwdAction(@RequestBody HashMap<String,String> hashMap) {
            String oriPwd = hashMap.get("oriPwd");
            String newPwd = hashMap.get("newPwd");
            String confirmPwd = hashMap.get("confirmPwd");
            if(!newPwd.equals(confirmPwd)){
                return ResultMessage.fail("新密码与确认密码不一致");
            }
            BlogUser blogUser = (BlogUser) httpSession.getAttribute("blogUser");
            if(!oriPwd.equals(blogUser.getUserPwd())){
                return ResultMessage.fail("原密码不正确");
            }
            blogUser.setUserPwd(newPwd);
            blogUserService.updBlogUser(blogUser);
            return ResultMessage.success();
        }
        @RequestMapping("/admin/delUser")
        public String delUser(String id){
            System.out.println(id);
            //控制访问      业务逻辑     数据库访问层
            //controller==>service ==>mapper
            //redirect forward
            blogUserService.delBlogUser(id);
            return "redirect:/admin/userlist";
        }

        @RequestMapping("/admin/getUserById")
        @ResponseBody
        public ResultMessage getUserById(@RequestBody HashMap<String, String> hashMap){
            String id = hashMap.get("id");
            BlogUser blogUser = blogUserService.getBlogUserById(id);
            List<BlogUser> data = new ArrayList<>();
            data.add(blogUser);
            ResultMessage resultMessage = new ResultMessage();
            resultMessage.setData(data);
            return resultMessage;
        }
}
